package com.wt.struggle.goodbye.user.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Message
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.ProUserInfo
import kotlinx.android.synthetic.main.account_safe_layout.*
import kotlinx.android.synthetic.main.head.*

/**
 * 账号安全
 */
class AccountSafeActivity : ProActivity() {
    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
        when (what) {
            Send.UPDATE_USER_CODE -> {

                if (netInfo.code == Config.SUCCESS) {
                    showToastShort("绑定成功")
                    val data = Intent()
                    data.putExtra("email", tvEmail.text.toString())
                    data.putExtra("phone", tvPhoneNumber.text.toString())
                    setResult(Activity.RESULT_OK, data)
                    finish()
                }
            }

        }

    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.account_safe_layout)

        tvTitle.text = "安全中心"

        imageBack.setOnClickListener {
            finish()
        }

        val userInfo = intent.getParcelableExtra<ProUserInfo>("userInfo")
        showUserInfo(userInfo)

        initClick()

    }

    private fun initClick() {
        imageEmailSure.setOnClickListener {
            val email = tvEmail.text.toString()
            if (email != "") {
                send!!.bangEmail(email, handler)
            } else {
                showToastShort("请输入邮箱账号")
            }

        }


        imagePhoneSure.setOnClickListener {

            val phone = tvPhoneNumber.text.toString()
            if (phone != "") {
                send!!.bangPhoneNumber(phone, "", handler)
            } else {
                showToastShort("请输入电话号码")
            }

        }

        helpLinear.setOnClickListener {

        }

        updatePhoneLinear.setOnClickListener {

        }
    }

    private fun showUserInfo(userInfo: ProUserInfo?) {
        if (userInfo != null) {
            tvPhoneNumber.setText(userInfo.mobile)
            tvEmail.setText(userInfo.email)
        }


    }
}